//
//  GMJsonWriteAndRead.m
//  news
//
//  Created by 雷国敏 on 16/9/27.
//  Copyright © 2016年 lieop. All rights reserved.
//

#import "GMJsonWriteAndRead.h"


@implementation GMJsonWriteAndRead


+ (void)gm_jsonWriteToFileWithDictionary:(id)params andFileName:(NSString*)fileName
{
	[GMJsonWriteAndRead CreatePathDirectory:@"JsonFiles"];
	if ([params isEqual:nil]) {
		return;
	}
	@try
	{
		[NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
	}@catch (NSException * e) {
		LxDBAnyVar( e);
		return;
	}
	
	NSData *dicData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *path = [paths objectAtIndex:0];
	
	NSString *writePath = [path stringByAppendingPathComponent:@"JsonFiles"];
	
	NSString *JsonPath = [writePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
	
	[dicData writeToFile:JsonPath atomically:YES];
	
}

+ (id)gm_jsonReadWithJsonFileName:(NSString*)fileName
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [docs[0] stringByAppendingPathComponent:@"JsonFiles"];
	
	NSString *JsonPath= [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
	NSString *encodeUrlString = [JsonPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	NSURL *pathDirectory = [NSURL URLWithString:encodeUrlString];
	
	if ([fileManager fileExistsAtPath:[pathDirectory path]]) {
		
		NSData *dataR = [NSData dataWithContentsOfFile:JsonPath];
		
		NSError *error = nil;
		id JsonObject=[NSJSONSerialization JSONObjectWithData:dataR
													  options:NSJSONReadingAllowFragments
														error:&error];
		if (error) {
			return nil;
		}else{
			return JsonObject;
		}
		
	}else{
		return nil;
	}
	
}

+ (void)CreatePathDirectory:(NSString*)directory
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
	NSURL *documentsDirectory = [URLs objectAtIndex:0];
	
	NSURL *pathDirectory = [documentsDirectory URLByAppendingPathComponent:directory];
	
	if (![fileManager fileExistsAtPath:[pathDirectory path]]) {
		[fileManager createDirectoryAtURL:pathDirectory withIntermediateDirectories:YES attributes:nil error:nil];
	}
	
}

+ (void)gm_removeAllJsonFile
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [docs[0] stringByAppendingPathComponent:@"JsonFiles"];
	
	if ([fileManager fileExistsAtPath:path]) {
		[fileManager removeItemAtPath:path error:nil];
	}
	
	
}

+ (BOOL)gm_isExistsJsonFileWithFileName:(NSString*)fileName
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [docs[0] stringByAppendingPathComponent:@"JsonFiles"];
	NSString *JsonPath= [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
	
	return [fileManager fileExistsAtPath:JsonPath];
}

+ (void)gm_removeJsonFileWithFileName:(NSString*)fileName
{
	if ([GMJsonWriteAndRead gm_isExistsJsonFileWithFileName:fileName]) {
		NSFileManager *fileManager = [NSFileManager defaultManager];
		NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *path = [docs[0] stringByAppendingPathComponent:@"JsonFiles"];
		NSString *JsonPath= [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
		
		[fileManager removeItemAtPath:JsonPath error:nil];
	}
}

+ (void)gm_appendDataSeekToEnd:(id)params withFileName:(NSString*)fileName;
{
	NSData *data = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [paths objectAtIndex:0];
	NSString *sourcePath = [path stringByAppendingPathComponent:@"JsonFiles"];
	NSString *JsonPath= [sourcePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
	
	NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:JsonPath];
	[fielHandle seekToEndOfFile]; // 将节点跳到文件的末尾
	
	[fielHandle writeData:data]; // 追加写入数据
	[fielHandle closeFile];
	
}

+ (void)gm_appendDataSeekToBegin:(id)params withFileName:(NSString*)fileName;
{
	NSData *data = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [paths objectAtIndex:0];
	NSString *sourcePath = [path stringByAppendingPathComponent:@"JsonFiles"];
	NSString *JsonPath= [sourcePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
	
	NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:JsonPath];
	
	NSData *oldData = [fielHandle readDataToEndOfFile];
	
	[fielHandle seekToFileOffset:0]; // 将节点跳到文件的开头
	
	[fielHandle writeData:data]; // 写入新数据
	
	[fielHandle seekToFileOffset:data.length];
	
	[fielHandle writeData:oldData]; // 写入原本的数据
	
	
	[fielHandle closeFile];
}


+ (void)gm_jsonWriteToPlistFileWithDictionaryOrArray:(id)params andFileName:(NSString *)fileName
{
	[GMJsonWriteAndRead CreatePathDirectory:@"plists"];
	
	NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [docs[0] stringByAppendingPathComponent:@"plists"];
	
	NSString *JsonPath= [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
	
	[params writeToFile:JsonPath atomically:YES];
}


+ (void)gm_jsonWriteToFileWithPlistName:(NSString*)plistName andFileName:(NSString*)fileName
{
	
	[GMJsonWriteAndRead CreatePathDirectory:@"JsonFiles"];
	
	NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@.plist",plistName] ofType:nil]];
	
	NSData *dicData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *path = [paths objectAtIndex:0];
	
	NSString *writePath = [path stringByAppendingPathComponent:@"JsonFiles"];
	
	NSString *JsonPath = [writePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.json",fileName]];
	
	[dicData writeToFile:JsonPath atomically:YES];
}


@end
